﻿using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Samples.Location;
using SimpleGPSApp.Base;

namespace SimpleGPSApp.Controls
{
    public partial class GpsInformation : UserControl
    {
        private string format = "{0}°{1}'{2:0}'' {3}";
        private decimal speed = 0;
        private char degreeSymbol = (char)176;
        private SpeedType speedType = SpeedType.Current;

        private GpsPosition position = null;

        public GpsPosition Position
        {
            get { return position; }
            set 
            { 
                position = value;
                if (position != null)
                {
                    this.UpdateGPSInformation();
                }
            }
        }

        public GpsInformation()
        {
            InitializeComponent();
        }

        private void UpdateGPSInformation()
        {
            if (position.LatitudeValid && position.LongitudeValid)
            {
                this.unitValueControl_longitude.Value = position.GetLongitude(format);
                this.unitValueControl_longitude.Refresh();
                this.unitValueControl_latitude.Value = position.GetLatitude(format);
                this.unitValueControl_latitude.Refresh();
                UpdateDistanceInfo();
            }

            if (position.SpeedValid)
            {
                speed = Math.Round((decimal)(position.Speed * 1.852), 1);

                ShowSpeed(speed);

                if (speed > Root.Statistics.MaxSpeed)
                {
                    Root.Statistics.MaxSpeed = speed;
                }
            }

            if (position.SatellitesInSolutionValid &&
                position.SatellitesInViewValid &&
                position.SatelliteCountValid)
            {
                this.unitValueControl_satelites.Value = position.GetSatelitesInfo();
                this.unitValueControl_satelites.Refresh();
            }

            if (position.HeadingValid)
            {
                this.unitValueControl_heading.Value = position.Heading.ToString() + degreeSymbol;
                this.unitValueControl_heading.Refresh();
                this.controlGPSCompass1.Angle = (int)position.Heading;
                this.controlGPSCompass1.Refresh();
            }

            if (position.SeaLevelAltitudeValid)
            {
                this.unitValueControl_altitude.Value = position.SeaLevelAltitude.ToString();
                this.unitValueControl_altitude.Refresh();
            }
        }

        private void UpdateDistanceInfo()
        {
            this.distanceDisplay1.CountDistance(this.position);
        }

        private void ShowSpeed(decimal speed)
        {
            switch (speedType)
            {
                case SpeedType.Current:
                    this.unitValueControlSpeed.ValueName = "V";
                    this.unitValueControlSpeed.Value = speed.ToString();
                    break;
                case SpeedType.Max:
                    this.unitValueControlSpeed.ValueName = "Vmax";
                    this.unitValueControlSpeed.Value = Root.Statistics.MaxSpeed.ToString();
                    break;
            }

            this.unitValueControlSpeed.Refresh();
        }

        private void unitValueControlSpeed_Click(object sender, EventArgs e)
        {
            if (speedType == SpeedType.Current)
            {
                speedType = SpeedType.Max;
            }
            else
            {
                speedType = SpeedType.Current;
            }

            ShowSpeed(speed);
        }


        private enum SpeedType
        {
            Current,
            Max,
        }
    }
}
